package com.work.cos.util.thirdpay.alipay;


import com.work.cos.util.thirdpay.alipay.config.AlipayServiceEnvConstants;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.work.cos.util.thirdpay.alipay.config.AlipayConfig2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * payment module
 *
 * @author hh
 */
//@Controller
public class Alipay
{




    private static String appID = AlipayServiceEnvConstants.APP_ID;
    private static String privateKey = AlipayServiceEnvConstants.PRIVATE_KEY;
    private static String charset = AlipayServiceEnvConstants.CHARSET;
    private static String productCode = "QUICK_MSECURITY_PAY";
    private static String method = "alipay.trade.app.pay";
    private static String signType = AlipayServiceEnvConstants.SIGN_TYPE;
    private static String version = "1.0";
    private static String notifyUrl = AlipayServiceEnvConstants.notify_url;


    private static final Logger logger	= LoggerFactory.getLogger(Alipay.class);

    public static String getAlipayInfo(String outTradeNo, String subject, String body, double price,String serviceName) throws AlipayApiException,UnsupportedEncodingException
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timestamp = sdf.format(new Date());

        Map<String,Object> map = new HashMap<>();
        map.put("timeout_express","90m");
        map.put("seller_id","");
        map.put("product_code",productCode);
        map.put("total_amount",price);
        map.put("subject",subject);
        map.put("body",body);
        map.put("out_trade_no",outTradeNo);
        map.put("passback_params",encode(serviceName));


        String bizContent = toJson(map);

        AlipayServiceEnvConstants as = new AlipayServiceEnvConstants();
        String notify_url = notifyUrl;// 增加支付异步通知回调,记住上下notify_url的位置,全在sign_type之前,很重要,同样放在最后都不行

        String content = "app_id=" + appID + "&biz_content=" + bizContent + "&charset=" + charset + "&format=json&method=" + method
                + "&notify_url=" + notify_url + "&sign_type=" + signType + "&timestamp=" + timestamp + "&version="
                + version;

        logger.debug("getAlipayInfo 原始content={}",content);


        String sign = AlipaySignature.rsaSign(content, privateKey, charset,signType);

        logger.debug("getAlipayInfo sign={}",sign);


        return "app_id=" + encode(appID) + "&biz_content=" + encode(bizContent) + "&charset="
                + encode(charset) + "&format=json&method=" + encode(method) + "&notify_url=" + encode(notify_url) + "&sign_type="
                + encode(signType) + "&timestamp=" + encode(timestamp) + "&version=" + encode(version) + "&sign="
                + encode(sign);


    }

    private static String encode(String sign) throws UnsupportedEncodingException {
        return URLEncoder.encode(sign, "utf-8").replace("+", "%20");
    }

    private static String toJson(Map<String,Object> map) {
        String context = "";
        context += "{" + "\"timeout_express\":\"" + map.get("timeout_express") + "\"," + "\"seller_id\":\""
                + map.get("seller_id") + "\"," + "\"product_code\":\"" + map.get("product_code") + "\","
                + "\"total_amount\":\"" + map.get("total_amount") + "\"," + "\"subject\":\"" + map.get("subject") + "\","
                + "\"body\":\"" + map.get("body") + "\","+ "\"passback_params\":\"" + map.get("passback_params") + "\"," + "\"out_trade_no\":\"" + map.get("out_trade_no") + "\"}";
        return context;
    }

    public static void main(String[] args) {
        String content = "a=123";
        try {
            String sign = AlipaySignature.rsaSign(content, privateKey, charset,signType);
            System.out.println(sign);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }

}
